package com.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dao.*;
import com.pojo.*;
import com.util.PasswordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LoginService {

    @Autowired
    private WuyeDAO wuyeDAO ;

    @Autowired
    private QiyeDAO qiyeDAO ;

    @Autowired
    private BusinessDAO businessDAO;

    @Autowired
    private RechargeStationDAO rechargeStationDAO;

    @Autowired
    private CertificateUserDao certificateUserDao;

    /**
     * 物业登录
     * @param username
     * @param password
     * @return
     */
    public Wuye wuyeLogin(String username , String password){
        List<Wuye> wuyes = wuyeDAO.selectList(new QueryWrapper<Wuye>().eq("phone" , username).eq("password" , password)) ;
        if(wuyes.size() != 0) {
            return wuyes.get(0) ;
        }
        return null ;
    }

    /**
     * 企业登录
     * @param username
     * @param password
     * @return
     */
    public Qiye qiyeLogin(String username , String password){
        List<Qiye> qiyes = qiyeDAO.selectList(new QueryWrapper<Qiye>().eq("phone" , username).eq("password" , password)) ;
        if(qiyes.size() != 0) {
            return qiyes.get(0) ;
        }
        return null ;
    }

    /**
     *
     * 充值点登录
     * @author zxj
     * @date 2021-09-01
     * @param username
     * @param password
     * @return
     */
    public RechargeStation rechargeStationLogin(String username , String password){
        QueryWrapper<RechargeStation> wrapper = new QueryWrapper();
        wrapper.eq("phone",username);
        wrapper.eq("loginPwd",password);
        wrapper.eq("status",1);
        List<RechargeStation> rechargeStationsList = rechargeStationDAO.selectList(wrapper) ;
        if(rechargeStationsList.size() != 0) {
            return rechargeStationsList.get(0);
        }
        return null ;
    }


    /**
     *
     * 证件管理登录
     * @param username
     * @param password
     * @return
     */
    public CertificateUser certificateUserLogin(String username , String password){
        QueryWrapper<CertificateUser> wrapper = new QueryWrapper();
        wrapper.eq("login_name",username);
        wrapper.eq("password",password);
        wrapper.eq("status",1);
        List<CertificateUser> certificateUserList = certificateUserDao.selectList(wrapper) ;
        if(certificateUserList.size() != 0) {
            return certificateUserList.get(0);
        }
        return null ;
    }

    /**
     * 证件管理用户修改密码
     * @param oldPwd
     * @param newPwd
     * @return
     */
    public String certificateUseUpdatePassword(String oldPwd , String newPwd , String id){
        CertificateUser certificateUser = certificateUserDao.selectById(id) ;
        if(!PasswordUtil.jiami(oldPwd).equals(certificateUser.getPassword())) {
            return "原始密码错误" ;
        }
        certificateUser.setPassword(PasswordUtil.jiami(newPwd));
        certificateUserDao.updateById(certificateUser) ;
        return null ;
    }



    /**
     * 充值点修改密码
     * @param oldPwd
     * @param newPwd
     * @return
     */
    public String rechargeStationUpdatePassword(String oldPwd , String newPwd , String id){
        RechargeStation rechargeStation = rechargeStationDAO.selectById(id) ;
        if(!PasswordUtil.jiami(oldPwd).equals(rechargeStation.getLoginPwd())) {
            return "原始密码错误" ;
        }
        rechargeStation.setLoginPwd(PasswordUtil.jiami(newPwd));
        rechargeStationDAO.updateById(rechargeStation) ;
        return null ;
    }

    /**
     * 修改密码
     * @param oldPwd
     * @param newPwd
     * @return
     */
    public String wyUpdatePassword(String oldPwd , String newPwd , String id){
        Wuye wuye = wuyeDAO.selectById(id) ;
        if(!PasswordUtil.jiami(oldPwd).equals(wuye.getPassword())) {
            return "原始密码错误" ;
        }
        wuye.setPassword(PasswordUtil.jiami(newPwd));
        wuyeDAO.updateById(wuye) ;
        return null ;
    }


    /**
     * 修改密码
     * @param oldPwd
     * @param newPwd
     * @return
     */
    public String businessUpdatePassword(String oldPwd , String newPwd , String id){
        Business business = businessDAO.selectById(id) ;
        if(!PasswordUtil.jiami(oldPwd).equals(business.getPassword())) {
            return "原始密码错误" ;
        }
        business.setPassword(PasswordUtil.jiami(newPwd));
        businessDAO.updateById(business) ;
        return null ;
    }

}
